package com.meiqiongproperty.yiguanjia.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.meiqiongproperty.yiguanjia.db.MyDB;
import com.meiqiongproperty.yiguanjia.db.table.UserInfoTable;
import com.meiqiongproperty.yiguanjia.httpbean.UserInfo;
import com.meiqiongproperty.yiguanjia.utils.LogUtil;

import java.util.ArrayList;
import java.util.Locale;

/**
 * userDao
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.db.dao
 * @User ChenJing
 * @Date 2017/5/22
 */

public class UserInfoDao implements UserInfoTable {
    private static final String TAG = UserInfoTable.class.getSimpleName();

    private SQLiteDatabase mDb;

    /**
     * 将上层的context传到Dao层
     *
     * @param context
     */
    public UserInfoDao(Context context) {
        mDb = MyDB.getInstance(context).getDB();
        if (mDb == null) {
            Log.e(TAG, "init SQLiteDatabase failed ");
        }
    }

    /**
     * 添加一条
     *
     * @param user
     * @return
     */
    public long add(UserInfo.UserinfoBean user) {
        // 入库成功后的id。
        long id = -1;
        if (null == user) {
            Log.w(TAG, "Add note:note is null!");
            return id;
        }
        // 入库数值。
        ContentValues values = new ContentValues();
        values.put(USERID, user.getId());
        values.put(LOGINNAME, user.getLoginName());
        values.put(MOBILE, user.getMobile());
        values.put(RELATION, user.getRelation());
        values.put(CREATEAT, user.getCreateAt());
        values.put(REALNAME,user.getRealName());
        values.put(SEX, user.getSex());
        values.put(BIRTHDAY, user.getBirthday());
        values.put(IS_ENABLE, user.getIs_enable());
        values.put(REMARK, user.getRemark());
        values.put(POINT, user.getPoint());
        values.put(AVATAR, user.getAvatar());
        values.put(DEVICE, user.getDevice());
        values.put(TOKEN, user.getToken());
        LogUtil.d("mDb：" + mDb);
        // 入库。
        id = mDb.insert(TABLE_NAME, null, values);
        return id;
    }

    /**
     * 通过ID更新
     *
     * @param id
     * @param user
     * @return
     */
    public int update(int id, UserInfo.UserinfoBean user) {
        int updateCount = 0;
        // 参数检查
        if (id < 0 || (user == null)) {
            Log.w(TAG, "update note,id = " + id);
            return updateCount;
        }
        // 匹配条件
        String whereClause = String.format("%s=?", UserInfoTable.USERID);
        String[] whereArgs = new String[]{String.valueOf(id)};
        // 入库值
        ContentValues values = new ContentValues();
        values.put(USERID, user.getId());
        values.put(LOGINNAME, user.getLoginName());
        values.put(MOBILE, user.getMobile());
        values.put(RELATION, user.getRelation());
        values.put(CREATEAT, user.getCreateAt());
        values.put(REALNAME,user.getRealName());
        values.put(SEX, user.getSex());
        values.put(BIRTHDAY, user.getBirthday());
        values.put(IS_ENABLE, user.getIs_enable());
        values.put(REMARK, user.getRemark());
        values.put(POINT, user.getPoint());
        values.put(AVATAR, user.getAvatar());
        values.put(DEVICE, user.getDevice());
        values.put(TOKEN, user.getToken());
        try {
            updateCount = mDb.update(UserInfoTable.TABLE_NAME, values, whereClause,
                    whereArgs);
        } catch (Exception e) {
            Log.e(TAG, "update note table error! count = " + updateCount);
        }
        return updateCount;
    }


    /**
     * 通过ID删除
     *
     * @param user_id
     * @return
     */
    public int delete(String user_id) {
        // 创建返回值
        int delCount = 0;
        // 参数检查
//        if (id < 0) {
//            Log.w(TAG, "query note,id = " + id);
//            return delCount;
//        }
        // 匹配条件
        String whereClause = String.format("%s=?", USERID);
        String[] whereArgs = new String[]{user_id};
        try {
            delCount = mDb.delete(UserInfoTable.TABLE_NAME, whereClause, whereArgs);
        } catch (Exception e) {
            Log.e(TAG, "delete note table error! id =" + delCount);
        }
        return delCount;
    }

    /**
     * 删除全部
     *
     * @return
     */
    public int deleteAll() {
        int delCount = 0;
        try {
            delCount = mDb.delete(UserInfoTable.TABLE_NAME, null, null);

        } catch (Exception e) {
            Log.e(TAG, "delete note table error! id =" + delCount);
        }
        return delCount;
    }

    /**
     * 通过ID查询
     *
     * @param id
     * @return
     */
    public UserInfo.UserinfoBean query(int id) {
        // 参数检查
        if (id < 0) {
            Log.w(TAG, "query note,id = " + id);
            return null;
        }
        // 创建返回值
        UserInfo.UserinfoBean user = null;
        // 查询条件
        String selection = String.format(Locale.US, "%s='%s'", USERID, id);
        // 获取游标
        Cursor cursor = null;
        try {
            cursor = mDb.query(TABLE_NAME, null, selection, null, null, null,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                // 将数值设置到实体类中
                user = new UserInfo.UserinfoBean();
                user.setId(cursor.getString(INDEX_USERID));
                user.setMobile(cursor.getString(INDEX_MOBILE));
                user.setRealName(cursor.getString(INDEX_REALNAME));
                user.setBirthday(cursor.getString(INDEX_BIRTHDAY));
                user.setAvatar(cursor.getString(INDEX_AVATAR));
                user.setCreateAt(cursor.getString(INDEX_CREATEAT));
                user.setPoint(cursor.getString(INDEX_POINT));
                user.setIs_enable(cursor.getString(INDEX_IS_ENABLE));
                user.setSex(cursor.getString(INDEX_SEX));
                user.setDevice(cursor.getString(INDEX_DEVICE));
                user.setRemark(cursor.getString(INDEX_REMARK));
                user.setRelation(cursor.getString(INDEX_RELATION));
                user.setToken(cursor.getString(INDEX_TOKEN));
                user.setLoginName(cursor.getString(INDEX_LOGINNAME));
            }
        } catch (SQLiteException e) {
            Log.e(TAG, "---Query failed!" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return user;
    }

    /**
     * 查询全部
     *
     * @return
     */
    public ArrayList< UserInfo.UserinfoBean> queryAll() {
        // 创建返回值
        ArrayList< UserInfo.UserinfoBean> userList = new ArrayList< UserInfo.UserinfoBean>();
        // 查询条件
        String order = USERID + " DESC";
        // 获取游标
        Cursor cursor = null;
        UserInfo.UserinfoBean user = null;
        try {
            cursor = mDb.query(TABLE_NAME, null, null, null, null, null, order);
            if (cursor != null && cursor.moveToFirst()) {
                // 循环取出所有数值
                do {
                    // 赋值
                    user = new UserInfo.UserinfoBean();
                    user.setId(cursor.getString(INDEX_USERID));
                    user.setMobile(cursor.getString(INDEX_MOBILE));
                    user.setRealName(cursor.getString(INDEX_REALNAME));
                    user.setBirthday(cursor.getString(INDEX_BIRTHDAY));
                    user.setAvatar(cursor.getString(INDEX_AVATAR));
                    user.setCreateAt(cursor.getString(INDEX_CREATEAT));
                    user.setPoint(cursor.getString(INDEX_POINT));
                    user.setIs_enable(cursor.getString(INDEX_IS_ENABLE));
                    user.setSex(cursor.getString(INDEX_SEX));
                    user.setDevice(cursor.getString(INDEX_DEVICE));
                    user.setRemark(cursor.getString(INDEX_REMARK));
                    user.setRelation(cursor.getString(INDEX_RELATION));
                    user.setToken(cursor.getString(INDEX_TOKEN));
                    user.setLoginName(cursor.getString(INDEX_LOGINNAME));
                    // 添加到集合中
                    userList.add(user);
                } while (cursor.moveToNext());
            }
        } catch (SQLiteException e) {
            Log.e(TAG, "Query all note failed!" + e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return userList;
    }
}
